package com.lsa.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsa.southwind.entity.Dormitory;
import com.lsa.southwind.entity.Moveout;
import com.lsa.southwind.entity.Student;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.mapper.DormitoryMapper;
import com.lsa.southwind.mapper.MoveoutMapper;
import com.lsa.southwind.mapper.StudentMapper;
import com.lsa.southwind.service.MoveoutService;
import com.lsa.southwind.util.CommonUtil;
import com.lsa.southwind.vo.MoveoutVo;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-02-25
 */
@Service
public class MoveoutServiceImpl extends ServiceImpl<MoveoutMapper, Moveout> implements MoveoutService {
    @Autowired
    private MoveoutMapper moveoutMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;

    /*学生迁出登记*/
    @Override
    public PageVo list(Integer page, Integer size) {
        Page<Student> studentPage = new Page<>(page, size);
        QueryWrapper<Student> studentqueryWrapper = new QueryWrapper<>();
        studentqueryWrapper.eq("state", "入住");
        Page<Student> resultPage = this.studentMapper.selectPage(studentPage, studentqueryWrapper);
        List<Student> studentList = resultPage.getRecords();
        //VO转换
        List<StudentVo> studentVOList = new ArrayList<>();
        for (Student student : studentList) {
            StudentVo StudentVo = new StudentVo();
            BeanUtils.copyProperties(student, StudentVo);
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            StudentVo.setDormitoryName(dormitory.getName());
            studentVOList.add(StudentVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(studentVOList);
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        /*进行分页操作*/
        Page<Student> StudentSearchPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Student> studentPage = null;
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("state", "入住");
        /*如果搜索框的值为null就选择查询全部*/
        if (searchForm.getValue().equals("")) {
            studentPage = this.studentMapper.selectPage(StudentSearchPage, studentQueryWrapper);

        } else {
            /*否则就模糊查询*/
            QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
            /*字段名,值*/
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            studentPage = this.studentMapper.selectPage(StudentSearchPage, queryWrapper);
        }
        /*因为前端要的是宿舍名，而我们后端查询出来的是宿舍的id所有我们要把他转化一下*/
        List<Student> studentList = studentPage.getRecords();
        //将Vo转换
        List<StudentVo> studentVoList = new ArrayList<>();
        for (Student student : studentList) {
            /*前端需要的实体类*/
            StudentVo studentVo = new StudentVo();
            /*属性的复制 左边为被复制的属性，右边是目标属性*/
            BeanUtils.copyProperties(student, studentVo);
            /*现在根据学生的宿舍id查出宿舍名*/
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVoList.add(studentVo);

        }
        PageVo pageVo = new PageVo();
        /*获取总条数*/
        pageVo.setTotal(studentPage.getTotal());
        /*存入查出的数据*/
        pageVo.setData(studentVoList);
        return pageVo;
    }

    @Override
    public Boolean moveout(Integer id, String reason) {
        Moveout moveout = new Moveout();
        Student student = this.studentMapper.selectById(id);
        moveout.setStudentId(student.getId());
        moveout.setDormitoryId(student.getDormitoryId());
        moveout.setReason(reason);
        moveout.setCreateDate(CommonUtil.createDate());
        //插入迁出信息
        int moveoutinsert = this.moveoutMapper.insert(moveout);
        if (moveoutinsert != 1) return false;
        //更新学生信息
        student.setState("迁出");
        int studentupdateById = this.studentMapper.updateById(student);
        if (studentupdateById != 1) return false;

        try {
            /*因为学生迁出了所有，他所住的宿舍数量+1*/
            this.dormitoryMapper.addAvailable(student.getDormitoryId());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /*学生迁出记录*/
    @Override
    public PageVo moveoutList(Integer page, Integer size) {
        Page<Moveout> moveoutPage = new Page<>(page, size);
        Page<Moveout> moveoutPageList = this.moveoutMapper.selectPage(moveoutPage, null);
        List<Moveout> moveoutPageListRecords = moveoutPageList.getRecords();
        List<MoveoutVo> moveoutVoList = new ArrayList<>();
        for (Moveout moveout : moveoutPageListRecords) {
            MoveoutVo moveoutVo = new MoveoutVo();
            BeanUtils.copyProperties(moveout, moveoutVo);
            Student student = this.studentMapper.selectById(moveout.getStudentId());
            moveoutVo.setStudentName(student.getName());
            Dormitory dormitory = this.dormitoryMapper.selectById(moveout.getDormitoryId());
            moveoutVo.setDormitoryName(dormitory.getName());
            moveoutVoList.add(moveoutVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(moveoutVoList);
        pageVo.setTotal(moveoutPageList.getTotal());
        return pageVo;
    }

    @Override
    public PageVo moveoutSearch(SearchForm searchForm) {
        Page<Moveout> moveoutPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Moveout> moveoutPageList = null;
        if (searchForm.getValue().equals("")) {
            moveoutPageList = this.moveoutMapper.selectPage(moveoutPage, null);
        } else {
            QueryWrapper<Moveout> queryWrapper = new QueryWrapper<>();
            if (searchForm.getKey().equals("studentName")) {
                List<Student> studentList = this.studentMapper.moveoutStudentLike(searchForm.getValue());
                List<Integer> idList = new ArrayList<>();
                for (Student student : studentList) {
                    idList.add(student.getId());
                }
                queryWrapper.in("student_id", idList);

            } else if (searchForm.getKey().equals("dormitoryName")) {
                QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
                dormitoryQueryWrapper.like("name", searchForm.getValue());
                List<Dormitory> dormitoryList = this.dormitoryMapper.selectList(dormitoryQueryWrapper);
                List<Integer> idList = new ArrayList<>();
                for (Dormitory dormitory : dormitoryList) {
                    idList.add(dormitory.getId());
                }
                queryWrapper.in("dormitory_id", idList);
            }
            moveoutPageList = this.moveoutMapper.selectPage(moveoutPage, queryWrapper);
        }
        List<MoveoutVo> moveoutVoList = new ArrayList<>();
        for (Moveout moveout : moveoutPageList.getRecords()) {
            MoveoutVo moveoutVo = new MoveoutVo();
            BeanUtils.copyProperties(moveout, moveoutVo);
            Student student = this.studentMapper.selectById(moveout.getStudentId());
            Dormitory dormitory = this.dormitoryMapper.selectById(moveout.getDormitoryId());
            moveoutVo.setStudentName(student.getName());
            moveoutVo.setDormitoryName(dormitory.getName());
            moveoutVoList.add(moveoutVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setTotal(moveoutPageList.getTotal());
        pageVo.setData(moveoutVoList);
        return pageVo;
    }
}

